import { getUrlParams,personName } from './utils';

// mock tableListDataSource
let tableListDataSource = [];
let tableListHisDataSource = [];
for (let i = 0; i < 5; i += 1) {
  tableListDataSource.push({
    id: i,
    avatar: ['https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png'][i % 2],
    name: personName[Math.floor(Math.random()*100)+i],
    source: (i % 2),
    messageType: (i % 3),
    content: `消息内容 ${i}`,
    translation: `翻译内容 ${i}`,
    sendDate: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
    addressCode: [87.562301+Math.floor(Math.random() * 10000)/100000 ,43.862426+Math.floor(Math.random() * 10000)/100000],
    audit:(i % 2),
    contactPerson:personName[i+Math.floor(Math.random() * 100)],
    contactPhone: ['18146496469', '18290821685','18290821681'][i % 3]
  });
}


for (let i = 0; i < 50; i += 1) {
  tableListHisDataSource.push({
    id: i,
    avatar: ['https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png'][i % 2],
    name: personName[Math.floor(Math.random()*100)+i],
    source: (i % 2),
    messageType: (i % 3),
    content: `消息内容 ${i}`,
    translation: `翻译内容 ${i}`,
    sendDate: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
    addressCode: [87.562301+Math.floor(Math.random() * 10000)/100000 ,43.862426+Math.floor(Math.random() * 10000)/100000],
    contactPerson:personName[i+Math.floor(Math.random() * 100)],
    contactPhone: ['18146496469', '18290821685','18290821681'][i % 3],
    anchorName: personName[Math.floor(Math.random()*100)+i],
    guideName: personName[Math.floor(Math.random()*100)+i],
    auditStatus:(i % 2),
    playStatus:(i % 2),
  });
}

export function getHisMsg(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = getUrlParams(url);

  let dataSource = [...tableListHisDataSource];

  //查询翻译内容关键字
  if (params.translation) {
    dataSource = dataSource.filter(data => data.translation.indexOf(params.translation) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function getMsg(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }
  const params = getUrlParams(url);
  let dataSource = [...tableListDataSource];

  const result = dataSource;

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export default {
  getMsg,
  getHisMsg,
};
